<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <#include "../common/head.ftl"/>
    <script src="/js/plugins/treeview/bootstrap-treeview.js"></script>
    <link rel="stylesheet" href="/js/plugins/treeview/bootstrap-treeview.css">

    <script>
        $(function () {
            $.get("/region/listRegionByParentId.do",function (topData) {

                console.log(topData);
                //加载地区列表
                $('#treeview').treeview({
                    color: "#428bca",
                    showTags: true,
                    data: [{
                        text: '地区',
                       nodes: topData,
                        lazyLoad:function (node) {
                           $.get("/region/listRegionByParentId.do",{parentId:node.id},function (subData) {
                               $('#treeview').treeview('addNode',[subData,node]);
                           });
                       },
                       onNodeSelected:function (event,node) {
                           $.get("/region/listRegionByParentId.do",{parentId: node.id},function (subData) {
                               var html="";
                               $.each(subData,function (index,item) {
                                   html +="<tr><td>"+(index+1)+"</td><td>"+item.text+"</td><td><a href='javascript:;' data-json='"+item.json+"' class='editBtn'>编辑</a></td><tr>";
                               })
                               $("#tbody").html(html);
                           });
                       }
                    }]
                })
            });


            $("#editForm").clearForm(true);
            var json = $(this).data("json");
            if(json){
                var parentNode = $('#treeview').treeview('getSelected')[0];
                $("#editForm input[name='id']").val(json.id);
                $("#editForm input[name='parentId']").val(json.parentId);
                $("#editForm input[name='name']").val(json.name);
                $("#editForm input[name='parentName']").val(parentNode.text);
                $("#editForm input[name='state']").val(json.state);
            }
            $("#inputModal").modal("show");


            $("#tbody").on("click",".editBtn",function () {
                $("#editForm").clearForm(true);
                $("#inputModal").modal("show");
            });

            //保存
            $("#save").click(function () {
                $("#editForm").ajaxSubmit(function (data) {
                    if(data.success){
                        window.location.reload();
                    }else{
                        $.messager.alert(data.msg);
                    }
                })
            })

            //点击添加
            $("#add").click(function () {
                $("#editForm").clearForm(true);

                var parentNode = $('#treeview').treeview('getSelected')[0];
                if (!parentNode){
                    $.messager.alert("必须选一个");
                    return;
                }
                $("#editForm input[name='parentId']").val(parentNode.id);
                $("#editForm input[name='parentName']").val(parentNode.text);
                $("#inputModal").modal("show");
            })
        });
    </script>
</head>
<body>

<!-- 模态框（Modal） -->
<div class="modal fade" id="inputModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">地区编辑</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/region/saveOrUpdate.do" method="post" id="editForm">
                    <input type="hidden" name="id">
                    <input type="hidden" name="parentId">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">上级名称：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="parentName" disabled>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-4 control-label">地区名称：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="name">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">推荐：</label>
                        <div class="col-sm-6">
                            <select class="form-control" name="state">
                                <option value="0">不推荐</option>
                                <option value="1">推荐</option>
                            </select>
                        </div>
                    </div>
                </form>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary btn-save" id="save">保存</button>
            </div>
        </div>
    </div>
</div>

<div class="container " style="margin-top: 20px">
    <#include "../common/top.ftl"/>
    <div class="row">
        <div class="col-sm-3">
            <#assign menu="region"/>
            <#include "../common/menu.ftl"/>
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">地区管理</h1>
                </div>
            </div>

            <div class="row">
                <div class="col-sm-4">
                    <div id="treeview"></div>
                    <div id="something"></div>

                </div>

                <div class="col-sm-8">
                    <a href="/region/input.do" class="btn btn-success" id="add">
                        <span class="glyphicon glyphicon-plus"></span> 添加
                    </a>
                    <table class="table table-striped table-hover">
                        <tr>
                            <th>编号</th>
                            <th>名称</th>
                            <th>操作</th>
                        </tr>
                        <tbody id="tbody">

                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>


</body>
</html>